/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame.controllers;

import Managers.ObjectManager;
import Managers.SoundManager;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.NiftyEventSubscriber;
import de.lessvoid.nifty.controls.Label;
import de.lessvoid.nifty.controls.ListBox;
import de.lessvoid.nifty.controls.SliderChangedEvent;
import de.lessvoid.nifty.controls.label.LabelControl;
import de.lessvoid.nifty.controls.listbox.ListBoxControl;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import java.util.List;
import mygame.model.Game;
import mygame.model.highscore.HighScore;
import mygame.model.highscore.HighScoreXML;

/**
 *
 * @author tommy
 */
public class GameSettings implements ScreenController {
    private Nifty settings;
    private Screen screen;
    private Game game;
    
    // temporarily hold sound and music volume until confirm or cancel operation
    // performed
    private float soundVolume;
    private float musicVolume;
    private List<HighScore> NormalHighScores, TimeHighScores;
    
    public GameSettings (Game game) {
        this.game = game;
    }
    
    @Override
    public void bind(Nifty nifty, Screen screen) {
        settings = nifty;
        this.screen = screen;
    }

    @Override
    public void onStartScreen() {
        System.out.println("Start Screen");
    }

    @Override
    public void onEndScreen() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public void startGame() {
        System.out.println("Game Started...");
        Element menuGUI = settings.getCurrentScreen().findElementByName("menuLayer");
        menuGUI.setVisible(false);
        
// TODO        No se que onda esto
//        try {
//            ObjectManager.configure(game, "conf/game.xml");
//            ObjectManager.getInstance().loadLevel(0);
            //ObjectManager.getInstance().initKeys();
            SoundManager.initAmbientMusic();
//        } catch (FileNotFoundException ex) {
//            Logger.getLogger(GameSettings.class.getName()).log(Level.SEVERE, null, ex);
//        }

    }
    
    public void showHighScores() {
        System.out.println("Showing Highscores...");
        
        Element menuGUI = settings.getCurrentScreen().findElementByName("menuLayer");
        menuGUI.setVisible(false);
        Element highscoreGUI = settings.getCurrentScreen().findElementByName("highscoreLayer");
        highscoreGUI.setVisible(true);
        
        // Caching de los highscores para evitar cargarlos del archivo constantemente
        if (this.NormalHighScores == null) {
            ListBox<HighScore> highscoresNormalListGUI = settings.getCurrentScreen().findControl("highscoresNormal", ListBoxControl.class);
            highscoresNormalListGUI.addAllItems(HighScoreXML.loadHighScores("highscoresNormal.xml"));
            this.NormalHighScores = highscoresNormalListGUI.getItems();
        }
        
        if (this.TimeHighScores == null) {
            ListBox<HighScore> highscoresTimeListGUI = settings.getCurrentScreen().findControl("highscoresTime", ListBoxControl.class);
            highscoresTimeListGUI.addAllItems(HighScoreXML.loadHighScores("highscoresTime.xml"));       
            this.TimeHighScores = highscoresTimeListGUI.getItems();
        }
    }
    
    
    public void showOptions() {
        System.out.println("Showing Options...");
        Element menuGUI = settings.getCurrentScreen().findElementByName("menuLayer");
        menuGUI.setVisible(false);
        Element configGUI = settings.getCurrentScreen().findElementByName("configLayer");
        configGUI.setVisible(true);
    }
    
    public void showMainMenu() {
         //TODO cuando ponga la highscore layer, ocultarla aca!
        Element menuGUI = settings.getCurrentScreen().findElementByName("menuLayer");
        menuGUI.setVisible(true);
        Element configGUI = settings.getCurrentScreen().findElementByName("configLayer");
        configGUI.setVisible(false);
        Element highscoreGUI = settings.getCurrentScreen().findElementByName("highscoreLayer");
        highscoreGUI.setVisible(false);
        
        // Unload highscores to save memory usage
        ListBox<HighScore> highscoresNormalListGUI = settings.getCurrentScreen().findControl("highscoresNormal", ListBoxControl.class);
        highscoresNormalListGUI.clear();
        
        ListBox<HighScore> highscoresTimeListGUI = settings.getCurrentScreen().findControl("highscoresTime", ListBoxControl.class);
        highscoresTimeListGUI.clear();
        
    }
    
    public void applyConfigChanges() {
        System.out.println("Saving sound and music parameters...");
        showMainMenu();
    }
    @NiftyEventSubscriber(id="soundVolumeSlider")
    public void onVolumeSliderChangedEvent(final String id, final SliderChangedEvent event) {
        this.soundVolume = event.getValue();
        System.out.println(this.soundVolume);
    }
    
    @NiftyEventSubscriber(id="musicVolumeSlider")
    public void onMusicSliderChangedEvent(final String id, final SliderChangedEvent event) {
        this.musicVolume = event.getValue();
        System.out.println(this.musicVolume);
    }
    
    public void setHealth(int health) {
        Label healthLabel = settings.getCurrentScreen().findControl("healthLabel", LabelControl.class);
        healthLabel.setText(String.valueOf(health) + "/100");
    }
    
    public void setScore(int score) {
        Label scoreLabel = settings.getCurrentScreen().findControl("scoreLabel", LabelControl.class);
        scoreLabel.setText("Score: " + String.valueOf(score));
    }
}
